﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<div style="margin-top: 20px; margin-left: 30px;">
</div>
<form id="form1">
    <div style="padding-top: 20px; margin-right: 30px;">
        <table class="form">
            <tr>
                <td class="formTitle"><span class="required">*</span>组织机构：</td>
                <td class="formValue formDdlSelectorTd">
                    <select id="OrganizeId" name="OrganizeId" class="form-control required">
                        <option value="">==请选择==</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="formTitle"><span class="required">*</span>权限编码：</td>
                <td class="formValue">
                    <input id="qxCode" name="qxCode" type="text" class="form-control required" placeholder="请输入编码" />
                </td>
            </tr>
            <tr>
                <th class="formTitle"><span class="required">*</span>权限名称：</th>
                <td class="formValue">
                    <input id="qxmc" name="qxmc" type="text" class="form-control" placeholder="" />
                </td>
            </tr>
            <tr>
                <td class="formTitle"><span class="required">*</span>类型：</td>
                <td class="formValue formDdlSelectorTd">
                    <select id="rel_lxcode" name="rel_lxcode" class="form-control required">
                        @*<option value="">==请选择==</option>*@
                        <option value="1">特殊药品</option>
                        <option value="2">收费大类</option>
                        <option value="3">抗生素</option>
                    </select>
                </td>
            </tr>
            <tr id="div_ypsx">
                <td class="formTitle"><span class="required">*</span>特殊药品：</td>
                <td class="formValue formDdlSelectorTd">
                    @Html.DropDownList("ypsxCode", Newtouch.Infrastructure.EnumYpsx.Mzyp.ToDescSelectList(((int)Newtouch.Infrastructure.EnumYpsx.Mzyp)), "==请选择==", new { @class = "form-control required" })
                </td>
            </tr>
            <tr id="div_sfdl">
                <td class="formTitle"><span class="required">*</span>收费大类：</td>
                <td class="formValue formDdlSelectorTd">
                    <select id="sfdlCode" name="sfdlCode" class="form-control required">
                        @*<option value="">==请选择==</option>*@
                    </select>
                </td>
            </tr>
            <tr id="div_kss">
                <td class="formTitle"><span class="required">*</span>抗生素：</td>
                <td class="formValue formDdlSelectorTd">
                    @Html.DropDownList("kssCode", Newtouch.Infrastructure.EnumKss.Fxzsyyw.ToDescSelectList(((int)Newtouch.Infrastructure.EnumKss.Fxzsyyw)), "==请选择==", new { @class = "form-control required" })
                </td>
            </tr>
            <tr>
                <td class="formValue">
                    <input id="rel_value" name="rel_value" type="text" class="form-control" placeholder="" style="display:none;"/>
                </td>
            </tr>
            <tr>
                <th class="formTitle" style="height: 35px;">状态：</th>
                <td class="formValue" style="padding-top: 1px;">
                    <div class="ckbox">
                        <input id="zt" name="zt" type="checkbox" checked="checked"><label for="zt">有效</label>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</form>

<script>
    var keyValue = $.request("keyValue");
    var optionValue = "";
    $(function () {
        initControl();
        if (!!keyValue) {
            $.najax({
                url: "/SysMedicineAuthority/GetFormJson",
                data: { keyValue: keyValue },
                dataType: "json",
                async: false,
                success: function (data) {
                    $("#form1").formSerialize(data);
                    optionValue = data.rel_value;
                    getSelectByLx();//根据类型切换下拉选择框
                }
            });
        }
        else {
            var newOrgId = $.request('orgId');
            if (newOrgId) {
                $("#OrganizeId").val(newOrgId).trigger('change');
            }
        }
    });

    function initControl() {
        //组织机构下拉框
        $("#OrganizeId").bindSelect({
            url: "/Organize/GetChildTreeSelectJson",
        });
        

        //组织对应的科室下拉框加载
        $("#OrganizeId").bind("change", function () {
            var organizeId = $(this).val();

            //收费大类绑定
            $("#sfdlCode").bindSelect({
                url: "/ChargeCategory/GetTreeSelectJson?organizeId=" + organizeId,
            });

        });
        
        getSelectByLx();//根据类型切换下拉选择框

        //类型下拉框改变
        $("#rel_lxcode").bind("change", function () {  //1属性 2收费大类
            getSelectByLx();//根据类型切换下拉选择框
        });
        
    }

    $('#qxCode').focus(function () {
        var organizeId = $("#OrganizeId").val();
        if (!!!keyValue && ($.trim($(this).val()) === '' || organizeId != anPrevOrg)) {
            anPrevOrg = organizeId;
            $.najax({
                url: "/HOME/GetNewFieldUniqueValue?topOrgIdIsStar=false&initFieldLength=8&fieldName=xt_ypqx.qxCode&orgId=" + organizeId + "&r=" + Math.random(),
                dataType: "json",
                cache: false,
                success: function (data) {
                    $('#qxCode').val("Q"+data.data);
                }
            });
        }
    });

    function submitForm() {
        if (!$('#form1').formValid()) {
            return false;
        }
        var postData = $("#form1").formSerialize();
        if (postData.rel_lxcode == 1) {
            postData.rel_value = $("#ypsxCode").val();
            postData.rel_lxywcode = "tsypbz";
            postData.rel_lxywdesc = "特殊药品";
        } else if (postData.rel_lxcode == 2) {
            postData.rel_value = $("#sfdlCode").val();
            postData.rel_lxywcode = "sfdlCode";
            postData.rel_lxywdesc = "收费大类";
        } else if (postData.rel_lxcode == 3) {
            postData.rel_value = $("#kssCode").val();
            postData.rel_lxywcode = "kssCode";
            postData.rel_lxywdesc = "抗生素";
        }
        $.submitForm({
            url: "/SysMedicineAuthority/SubmitForm?keyValue=" + keyValue,
            param: postData,
            success: function () {
                $.currentWindow().$("#gridList").resetSelection();
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        })
    }

    function getSelectByLx() {
        var lx = $("#rel_lxcode").val();
        if (lx == 1) {//获取药品属性
            $("#div_ypsx").show();
            $("#div_sfdl").hide();
            $("#div_kss").hide();
            if (optionValue != "") {
                $("#ypsxCode").find("option[value =" + optionValue + "]").attr("selected", true);
            }
        }
        else if (lx == 2) {//获取收费大类
            $("#div_ypsx").hide();
            $("#div_sfdl").show();
            $("#div_kss").hide();
            if (optionValue != "") {
                $("#sfdlCode").val(optionValue).select2();
            }
        } else if (lx == 3) {//抗生素
            $("#div_ypsx").hide();
            $("#div_sfdl").hide();
            $("#div_kss").show();
            if (optionValue != "") {
                $("#kssCode").find("option[value ="+optionValue+"]").attr("selected", true);
            }
        }
    }
</script>